package bdhub.littletail.app.utils;

import com.bdhub.im.server.protobuf.MessageProtobuf;
import com.google.protobuf.InvalidProtocolBufferException;

import java.io.UnsupportedEncodingException;
import java.util.List;

import bdhub.littletail.app.message.AppMessage;
import bdhub.littletail.app.message.Head;
import bdhub.littletail.lib.utils.StringUtil;


public class MessageUtils {

	/**
	 * 通过protobuf消息对象获取业务消息对象
	 * 
	 * @param protoBufMessage
	 * @return
	 */
	public static AppMessage getMessageByProtoBuf(
			MessageProtobuf.CMsg protoBufMessage) {
		AppMessage message = new AppMessage();
		Head head = new Head();
		MessageProtobuf.CMsgHead protoHead = protoBufMessage.getHead();
		head.setType(protoHead.getType());
		head.setTextType(protoHead.getTextType());
		head.setMid(protoHead.getMid());
		head.setFromId(protoHead.getFromId());
		head.setToId(protoHead.getToId());
		head.setTime(Long.parseLong(protoHead.getTime()));
		head.setComments(protoHead.getComments());
		message.setHead(head);
		message.setBody(protoBufMessage.getBody());
		return message;
	}

	/**
	 * 根据业务消息对象获取protoBuf消息对象
	 * 
	 * @param message
	 * @return
	 */
	public static MessageProtobuf.CMsg getProtoBufMessageByMessage(
			AppMessage message) {
		return getProtoBufMessageBuilderByMessage(message).build();
	}
	
	/**
	 * 根据业务消息对象获取protoBuf消息对应的builder
	 * @param message
	 * @return
	 */
	public static MessageProtobuf.CMsg.Builder getProtoBufMessageBuilderByMessage(AppMessage message){
		MessageProtobuf.CMsg.Builder builder = MessageProtobuf.CMsg
				.newBuilder();
		MessageProtobuf.CMsgHead.Builder headBuilder = MessageProtobuf.CMsgHead.newBuilder();
		headBuilder.setType(message.getHead().getType());
		headBuilder.setTextType(message.getHead().getTextType());
		if (!StringUtil.isEmpty(message.getHead().getMid()))
			headBuilder.setMid(message.getHead().getMid());
		if (!StringUtil.isEmpty(message.getHead().getFromId()))
			headBuilder.setFromId(message.getHead().getFromId());
		if (!StringUtil.isEmpty(message.getHead().getToId()))
			headBuilder.setToId(message.getHead().getToId());
		if (message.getHead().getTime() != 0)
			headBuilder.setTime(String.valueOf(message.getHead().getTime()));
		if (!StringUtil.isEmpty(message.getHead().getComments()))
			headBuilder.setComments(message.getHead().getComments());
		if (!StringUtil.isEmpty(message.getBody()))
			builder.setBody(message.getBody());
		builder.setHead(headBuilder);
		return builder;
		
	}

	/**
	 * 将业务消息类型转换成string
	 * 
	 * @param message
	 * @return
	 */
	public static byte[] messageEncoding(AppMessage message) {
		return getProtoBufMessageByMessage(message).toByteArray();
	}

	/**
	 * 将业务消息类型转换成string
	 * 
	 * @param messages
	 * @return
	 */
	public static byte[][] messagesEncoding(List<AppMessage> messages) {
		byte[][] strs = new byte[messages.size()][];
		int index = 0;
		for (AppMessage message : messages) {
			strs[index++] = messageEncoding(message);
		}
		return strs;
	}

	/**
	 * 将string类型转换成消息对象
	 * 
	 * @param bt
	 * @return
	 * @throws InvalidProtocolBufferException
	 */
	public static AppMessage messageDecoding(byte[] bt) throws InvalidProtocolBufferException {
			return MessageUtils.getMessageByProtoBuf(MessageProtobuf.CMsg
					.parseFrom(bt));
	}

	public static void main(String[] args)
			throws InvalidProtocolBufferException, UnsupportedEncodingException {
//		Object m =null;
//		System.out.println(String.valueOf(m).length());
		System.out.println(1024*1024);
//		MessageProto.Message.Builder message = MessageProto.Message
//				.newBuilder();
//		MessageProto.Head.Builder head = MessageProto.Head.newBuilder();
//		head.setType(123);
//		message.setHead(head);
//		
//		MessageProto.Message msg = message.build();
////		MessageProto.Head.Builder headBuilder=MessageProto.Head.newBuilder();
////		headBuilder.setFromId("1323");
////	    message1.setBody(msg.getBody());
////	    message1.setHead(MessageProto.Head.parseFrom(msg.getHead().toByteString()));
//		MessageProto.Head.Builder headBuilder=msg.getHead().toBuilder().setFromId("1111");
//		msg= msg.toBuilder().setHead(headBuilder).build();
////		MessageProto.Message.newBuilder().setBody(msg.getBody()).setHead();
////		head = message.getHead().toBuilder();
////		head.setType(456);
//
//		System.out.println(msg.getHead().getFromId());
	}
}
